package com.mdlib.helper.base;

import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.view.View;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ljlib.help.R;
import com.mdlib.helper.ui.widget.CommonTopBar;

/**
 * Created by librabin on 16/11/15.
 */

public abstract class BaseTitleFragment extends BaseAppFragmentV2 {


    private CommonTopBar mTitlebar;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_title;
    }

    @Override
    protected void onBindViewBefore(View rootView) {
        super.onBindViewBefore(rootView);
        ViewStub stub = (ViewStub) rootView.findViewById(R.id.layout_content);
        stub.setLayoutResource(getContentLayoutId());
        stub.inflate();
    }


    protected abstract
    @LayoutRes
    int getContentLayoutId();

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        mTitlebar = (CommonTopBar) rootView.findViewById(R.id.top_bar);
    }

    public BaseTitleFragment setTitleText(String title) {
        if (mTitlebar != null) {
            mTitlebar.setTextCenter(title);
        }

        return this;
    }

    public BaseTitleFragment setCommonTitle(@StringRes int titleId) {
        return setTitle(titleId, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
    }


    public BaseTitleFragment setTitle(String titleText, @ColorRes int titleColorId) {
        if (mTitlebar != null) {
            TextView titleField = (TextView) mTitlebar.findViewById(R.id.tv_title_center);
            titleField.setText(titleText);
            titleField.setTextColor(getResources().getColor(titleColorId));
        }
        return this;
    }

    public BaseTitleFragment setTitle(@StringRes int titleId, @ColorRes int titleColorId) {
        if (mTitlebar != null) {
            TextView titleField = (TextView) mTitlebar.findViewById(R.id.tv_title_center);
            titleField.setText(getResources().getString(titleId));
            titleField.setTextColor(getResources().getColor(titleColorId));
        }
        return this;
    }


    public BaseTitleFragment setTitleText(@StringRes int titleId) {
        if (mTitlebar != null) {
            mTitlebar.setTextCenter(getResources().getString(titleId));
        }
        return this;
    }

    public BaseTitleFragment setTitleColor(@ColorRes int titleColorId) {
        if (mTitlebar != null) {
            TextView titleText = (TextView) mTitlebar.findViewById(R.id.tv_title_center);
            titleText.setTextColor(getResources().getColor(titleColorId));
        }
        return this;
    }

    public BaseTitleFragment setTitleBgColor(@ColorRes int bgColorId) {
        if (mTitlebar != null) {
            ImageView bgView = (ImageView) mTitlebar.findViewById(R.id.iv_title_bg);
            if (bgView != null) {
                if (bgColorId == 0) {
                    bgView.setBackgroundColor(getResources().getColor(R.color.white));
                } else {
                    bgView.setBackgroundColor(getResources().getColor(bgColorId));
                }
            }
//            mTitlebar.setBackgroundColor(getResources().getColor(bgColorId));
        }

        return this;
    }

    /**
     * 设置左边图片
     *
     * @param id
     * @return
     */
    public BaseTitleFragment setImgLeftBg(@DrawableRes int id) {
        if (mTitlebar != null) {
            mTitlebar.setImgLeft(id);
            mTitlebar.setShowImgLeft(true);
        }

        return this;
    }


    /**
     * 设置左边图片 监听器
     */
    public BaseTitleFragment setImgLeftListener(View.OnClickListener listener) {
        if (mTitlebar != null) {
            FrameLayout leftLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_left);
            leftLayout.setOnClickListener(listener);
        }
        return this;
    }

    /**
     * 设置右边图片
     *
     * @param id
     * @return
     */
    public BaseTitleFragment setImgRightBg(@DrawableRes int id) {
        if (mTitlebar != null) {
            mTitlebar.setImgRight(id);
            mTitlebar.setShowImgRight(true);
        }

        return this;
    }

    public BaseTitleFragment setTvRightBg(@StringRes int id) {
        if (mTitlebar != null) {
            mTitlebar.setTextRight(getResources().getString(id));
            mTitlebar.setShowImgRight(false);
        }

        return this;
    }

    public BaseTitleFragment setImgRightListener(View.OnClickListener listener) {
        if (mTitlebar != null) {
            FrameLayout rightLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_right);
            rightLayout.setOnClickListener(listener);
        }
        return this;
    }


    public BaseTitleFragment hideTitle() {
        if (mTitlebar != null) {
            mTitlebar.setVisibility(View.GONE);
        }
        return this;
    }




}
